package com.ruoyi.media.service.impl;

import java.time.*;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.constant.MediaConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.media.domain.Dailyreports;
import com.ruoyi.media.domain.dto.ReportIssuesDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.media.mapper.ReportIssuesMapper;
import com.ruoyi.media.domain.ReportIssues;
import com.ruoyi.media.service.IReportIssuesService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.constant.MediaConstants.HAS_OPEN_ISSUES_NO;
import static com.ruoyi.common.constant.MediaConstants.HAS_OPEN_ISSUES_YES;

/**
 * 日报问题详情Service业务层处理
 * 
 * @author crocodilePole
 * @date 2025-03-03
 */
@Slf4j
@Service
public class ReportIssuesServiceImpl implements IReportIssuesService 
{
    @Autowired
    private ReportIssuesMapper reportIssuesMapper;
    @Autowired
    private DailyreportsServiceImpl dailyreportsService;
    /**
     * 查询日报问题详情
     * 
     * @param id 日报问题详情主键
     * @return 日报问题详情
     */
    @Override
    public ReportIssues selectReportIssuesById(Long id)
    {
        return reportIssuesMapper.selectReportIssuesById(id);
    }

    /**
     * 查询日报问题详情列表
     * 
     * @param reportIssues 日报问题详情
     * @return 日报问题详情
     */
    @Override
    public List<ReportIssues> selectReportIssuesList(ReportIssues reportIssues)
    {

        ReportIssuesDto reportIssuesDto = BeanUtil.copyProperties(reportIssues, ReportIssuesDto.class);


        // 如果reportIssues.getCreateTime()存在
        if (reportIssues.getCreateTime() != null) {
            ///获取当前日期最小时间和最大时间
            Date date = reportIssues.getCreateTime();
            // 将 Date 转换为 Instant
            Instant instant = date.toInstant();
            // 使用系统默认时区将 Instant 转换为 LocalDate
            LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
            // 当天的开始时间是 00:00:00
            LocalDateTime startOfDay = localDate.atStartOfDay();
            // 当天的结束时间是 23:59:59.999999999
            LocalDateTime endOfDay = localDate.atTime(LocalTime.MAX);
            log.info("开始时间：{}", startOfDay);
            log.info("结束时间：{}", endOfDay);

            reportIssuesDto.setStartTime(startOfDay);
            reportIssuesDto.setEndTime(endOfDay);
        }

        return reportIssuesMapper.selectReportIssuesList(reportIssuesDto);
    }

    /**
     * 新增日报问题详情
     * 
     * @param reportIssues 日报问题详情
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertReportIssues(ReportIssues reportIssues)
    {
        Dailyreports dailyreports = dailyreportsService.selectDailyreportsById(reportIssues.getDailyReportId());
//        判断是否有该日报
        if (dailyreports == null) {
            throw new ServiceException("日报不存在");
        }

//        判断问题状态是否为待办or进行中,且日报没有未解决问题时
        if (
                (Objects.equals(reportIssues.getStatus(), MediaConstants.ISSUE_STATUS_TO_DO)
                        || Objects.equals(reportIssues.getStatus(), MediaConstants.ISSUE_STATUS_IN_PROGRESS))
                && dailyreports.getHasOpenIssues() == MediaConstants.HAS_OPEN_ISSUES_NO

        ) {
//            修改日报为存在未解决的问题
            dailyreports.setHasOpenIssues(HAS_OPEN_ISSUES_YES);
            dailyreportsService.updateDailyreports(dailyreports);
        }


        return reportIssuesMapper.insertReportIssues(reportIssues);
    }

    /**
     * 修改日报问题详情
     * 
     * @param reportIssues 日报问题详情
     * @return 结果
     */
    @Override
    public int updateReportIssues(ReportIssues reportIssues)
    {

        Dailyreports dailyreports = dailyreportsService.selectDailyreportsById(reportIssues.getDailyReportId());
        // 判断是否有该日报
        if (dailyreports == null) {
            throw new ServiceException("日报不存在");
        }

        int result = reportIssuesMapper.updateReportIssues(reportIssues);

        //判断问题状态是否为已完成
        if (!Objects.equals(reportIssues.getStatus(), MediaConstants.ISSUE_STATUS_COMPLETED)) {
            return result;
        }

        //日报不存在未解决问题时
        if (!(dailyreports.getHasOpenIssues() == HAS_OPEN_ISSUES_YES)) {
            return result;
        }

       //根据日报ID查询出所有问题
        ReportIssuesDto reportIssuesDto = new ReportIssuesDto();
        reportIssuesDto.setDailyReportId(reportIssues.getDailyReportId());
        List<ReportIssues> reportIssuesList =
                reportIssuesMapper.selectReportIssuesList(reportIssuesDto);

        //是否有未完成的问题
        int flag = 0;
        for (ReportIssues reportIssue : reportIssuesList) {
            if(!Objects.equals(reportIssue.getStatus(), MediaConstants.ISSUE_STATUS_COMPLETED)){
                flag = 1;
                break;
            }
        }



        //如果没有未完成的问题,修改日报为没有未解决问题
        if(flag == 0){
            dailyreports.setHasOpenIssues(HAS_OPEN_ISSUES_NO);
            log.info("{}",dailyreports);
            dailyreportsService.updateDailyreports(dailyreports);
        }



        return result;
    }

    /**
     * 批量删除日报问题详情
     * 
     * @param ids 需要删除的日报问题详情主键
     * @return 结果
     */
    @Override
    public int deleteReportIssuesByIds(Long[] ids)
    {
        return reportIssuesMapper.deleteReportIssuesByIds(ids);
    }

    /**
     * 删除日报问题详情信息
     * 
     * @param id 日报问题详情主键
     * @return 结果
     */
    @Override
    public int deleteReportIssuesById(Long id)
    {
        return reportIssuesMapper.deleteReportIssuesById(id);
    }
}
